package io.renren.common.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.common.collect.Maps;
import io.renren.common.config.BaseConstant;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author Singer create by singer email:313402703@qq.com
 * @projectName map-clothes-app-api
 * @Description: 阿里短信发送工具
 * @date 2019-09-16
 */
@Component
public class AliMessageSendComponent {


    @Resource
    private BaseConstant baseConstant;


    /**
     * @param phone       手机号码
     * @param messageCode 短信验证码
     * @return response
     * @description: 发送APP登陆阿里云短信验证码
     * @auther: create by singer - 王 浩 email:313402703@qq.com
     * @date 2019-09-16
     */
    public CommonResponse sendLoginMessageCode(String phone, String messageCode) throws Exception {
        return sendAliMessageCode(phone, messageCode, baseConstant.getAliMessageSignName(), baseConstant.getAliMessageLoginTemplateCode());
    }

    /**
     * @param phone 手机号码
     * @return response
     * @description: 骑手接单通知
     * @auther:ma
     * @date 2020-04-29
     */
   public CommonResponse sendRiderMessage(String phone, String riderName, String userOrderNO) throws Exception {
        return sendAliMessageRiderNotification(phone, riderName, userOrderNO, baseConstant.getAliMessageSignName(), baseConstant.getAliMessageRiderTemplateCode());
    }

    /**
     * @param phone 手机号码
     * @return response
     * @description: 商家接单通知
     * @auther:ma
     * @date 2020-04-29
     */
    public CommonResponse sendMerchantMessage(String phone, String shopName, String userOrderNO) throws Exception {
        return sendAliMessageMerchantNotification(phone, shopName, userOrderNO, baseConstant.getAliMessageSignName(), baseConstant.getAliMessageMerchantTemplateCode());
    }

    /**
     * @description: 发送APP忘记密码阿里云短信验证码
     * @auther: create by singer - 王 浩 email:313402703@qq.com
     * @date 2019-09-16
     * @param phone 手机号码
     * @param messageCode 短信验证码
     * @return response
     */
    /*public CommonResponse sendForgetPwdMessageCode(String phone, String messageCode) throws Exception{
        return sendAliMessageCode(phone,messageCode,baseConstant.getAliMessageSignName(),baseConstant.getAliMessageForgetPwdTemplateCode());
    }*/

    /**
     * @param phone       手机号码
     * @param messageCode 短信验证码
     * @return response
     * @description: 发送APP注册阿里云短信验证码
     * @auther: create by singer - 王 浩 email:313402703@qq.com
     * @date 2019-09-16
     */
    public CommonResponse sendRegMessageCode(String phone, String messageCode) throws Exception {
        return sendAliMessageCode(phone, messageCode, baseConstant.getAliMessageSignName(), baseConstant.getAliMessageTemplateRegCode());
    }


    /**
     * @param phone        手机号码
     * @param signName     签名名称
     * @param templateCode 模版code
     * @return response
     * @description: 骑手接单通知
     * @auther: ma
     * @date 2020-04-29
     */
    public CommonResponse sendAliMessageRiderNotification(String phone, String riderName, String userOrderNO, String signName, String templateCode) throws Exception {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                baseConstant.getAliAccessKeyId(),
                baseConstant.getAliAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
//        request.setDomain(baseConstant.getAliMessageSendUrl());
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);

        //短信
        HashMap<Object, Object> templateCodeMap = Maps.newHashMap();

        templateCodeMap.put("riderName", riderName);
        templateCodeMap.put("userOrderNO", userOrderNO);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(templateCodeMap));

        CommonResponse response = client.getCommonResponse(request);

        return response;
    }


    /**
     * @param phone        手机号码
     * @param signName     签名名称
     * @param templateCode 模版code
     * @return response
     * @description: 商家接单通知
     * @auther: ma
     * @date 2020-04-29
     */
    public CommonResponse sendAliMessageMerchantNotification(String phone, String shopName, String userOrderNO, String signName, String templateCode) throws Exception {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                baseConstant.getAliAccessKeyId(),
                baseConstant.getAliAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
//        request.setDomain(baseConstant.getAliMessageSendUrl());
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);

        //短信
        HashMap<Object, Object> templateCodeMap = Maps.newHashMap();

        templateCodeMap.put("shopName", shopName);
        templateCodeMap.put("userOrderNO", userOrderNO);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(templateCodeMap));

        CommonResponse response = client.getCommonResponse(request);

        return response;
    }

    /**
     * @param phone        手机号码
     * @param messageCode  短信验证码
     * @param signName     签名名称
     * @param templateCode 模版code
     * @return response
     * @description: 发送APP阿里云短信验证码
     * @auther: create by singer - 王 浩 email:313402703@qq.com
     * @date 2019-09-16
     */
    public CommonResponse sendAliMessageCode(String phone, String messageCode, String signName, String templateCode) throws Exception {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                baseConstant.getAliAccessKeyId(),
                baseConstant.getAliAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
//        request.setDomain(baseConstant.getAliMessageSendUrl());
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);

        //短信
        HashMap<Object, Object> templateCodeMap = Maps.newHashMap();

        templateCodeMap.put("code", messageCode);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(templateCodeMap));

        CommonResponse response = client.getCommonResponse(request);

        return response;
    }


}
